/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.aun.news;

import br.org.groupwareworkbench.core.exceptions.ConfigurationException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionMysql {

    private static final String hostName = "127.0.0.1";
    private static final String userName = "groupwareUser";
    private static final String password = "102938";
    private static final String jdbcDriver = "org.gjt.mm.mysql.Driver";
    private static final String dataBaseName = "noticias_original_AUN";
    private static final String dataBasePrefix = "jdbc:mysql://";
    private static final int dabaBasePort = 3306;
    private static final String url = dataBasePrefix + hostName + ":" + dabaBasePort + "/" + dataBaseName + "?useUnicode=true&characterEncoding=utf8";

    private Connection conn = null;

    public ConnectionMysql() {
    }

    public Connection getConnection() throws SQLException {
        try {
            if (conn == null) {
                Class.forName(jdbcDriver);
                conn = DriverManager.getConnection(url, userName, password);
            } else if (conn.isClosed()) {
                conn = null;
                return getConnection();
            }
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException(e);
        }
        return conn;
    }

    public void close() throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }
}
